//
//  EJSPayWebViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/6/8.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSPayWebViewController.h"
#import "EJSPreparePayForOrderRequest.h"
#import "EJSOrderListViewController.h"

#import <WebKit/WebKit.h>
#import "JSONKit.h"
#import <AFNetworking.h>

@interface EJSPayWebViewController ()<WKNavigationDelegate>
@property(strong , nonatomic) WKWebView *wkWebView;

@end

@implementation EJSPayWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"在线支付";
    
    self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.wkWebView];
    [self.wkWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.right.equalTo(self.view);
    }];
    self.wkWebView.navigationDelegate = self;
    
    [self loadRequest];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    FlyMainTabBarViewController *tab =  (FlyMainTabBarViewController*)UIApplication.sharedApplication.keyWindow.rootViewController;
//    [tab.csButton setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    FlyMainTabBarViewController *tab =  (FlyMainTabBarViewController*)UIApplication.sharedApplication.keyWindow.rootViewController;
//    [tab.csButton setHidden:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)leftBarButtonAction:(id)sender{
    EJSOrderListViewController *orderVC = [[EJSOrderListViewController alloc] init];
    orderVC.isNeedPopToRoot = YES;
    orderVC.orderType = @"1";
//    [self.navigationController pushViewController:orderVC animated:YES];
     [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)loadRequest{
    NSURL *url = [NSURL URLWithString:self.redsys.form_action];
    NSString *bodyString = [NSString stringWithFormat:@"Ds_Signature=%@&Ds_MerchantParameters=%@&Ds_SignatureVersion=%@",[self.redsys.Ds_Signature URLEncodedString],[self.redsys.Ds_MerchantParameters URLEncodedString],[self.redsys.Ds_SignatureVersion URLEncodedString]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: [bodyString dataUsingEncoding:NSUTF8StringEncoding]];
    [self.wkWebView loadRequest:request];
    [[HUDHelper sharedInstance] syncLoading];
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
   [[HUDHelper sharedInstance] syncLoading];
}
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
    
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    [[HUDHelper sharedInstance] syncStopLoading];
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation{
    
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [[HUDHelper sharedInstance] syncStopLoading];
    if([webView.URL.absoluteString containsString:@"success"]){
        [HUDHelper alert:@"支付成功" action:^{
            EJSOrderListViewController *orderVC = [[EJSOrderListViewController alloc] init];
            orderVC.orderType = @"2";
            orderVC.isNeedPopToRoot = YES;
//            [self.navigationController pushViewController:orderVC animated:YES];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
    }else if ([webView.URL.absoluteString containsString:@"fail"]){
        [HUDHelper alert:@"支付失败" action:^{
            EJSOrderListViewController *orderVC = [[EJSOrderListViewController alloc] init];
            orderVC.isNeedPopToRoot = YES;
            orderVC.orderType = @"1";
//            [self.navigationController pushViewController:orderVC animated:YES];
             [self.navigationController popToRootViewControllerAnimated:YES];
        }];
    }
}
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    [[HUDHelper sharedInstance] syncStopLoading];
}

@end
